package com.df.my_net_game.logic.equi;

import com.baidu.bjf.remoting.protobuf.Codec;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.df.my_net_game.game_rpc.EzGameException;
import com.df.my_net_game.game_rpc.service.annotation.LogicController;
import com.df.my_net_game.game_rpc.service.annotation.LogicMapping;
import com.df.my_net_game.logic.LogicIdDefine;
import com.df.my_net_game.logic.equi.handlers.Equip_UpLevelService;
import com.df.my_net_game.net.base.EZGameCtx;
import com.df.my_net_game.proto.equip.C2G_EquipUpLevel;
import com.df.my_net_game.proto.equip.G2C_EquipUpLevel;
import com.df.my_net_game.services.ThirdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

/**
 * TODO 全局异常处理
 */
@LogicController(name = "装备功能", desc = "装备相关操作",id = LogicIdDefine.Equip)
public class EquipController {
    @Autowired
    Equip_UpLevelService equip_UpLevelService;

    //被调用服务最好是无状态的
    @Autowired
    ThirdService thirdService;

    /**
     * 装备升级
     * @param req
     */
    @LogicMapping(desc = "装备升级",id = 1)
    public G2C_EquipUpLevel equipUpLevel(@Validated C2G_EquipUpLevel req) {
        Codec<C2G_EquipUpLevel> c2GEquipUpLevelCodec = ProtobufProxy.create(C2G_EquipUpLevel.class);
        //127.0.0.1
        //协议：长度(4) header(msgid(4),tag(1[0用户客户端1服务客户端]),reqId(4)，ip(4)) body(序列化后的数据)
        //协议：长度+header+body
        //长度:全消息长度(4)
        //header: header长度(4)+map
        //body:二进制数据
        //序列化：protobuf
//        thirdService.checkOrder(1);
//        Future future = thirdService.checkOrderAync(1);
//        boolean await = future.await(1000);
        //TODO 指定具体服务进行发送
//        ServiceMrg.send(serviceInstance,()->{
//            thirdService.checkOrder(1);
//        });
        //模拟请求(tcp/ws/udp)
//        //TODO 给服务广播(同步/异步 无返回值/有返回值)
//        EZGameCtx.getContext().broadCast("third",()->{
//            thirdService.checkOrder(1);
//        });
//        ServiceMrg.broadCastAsync("third",()->{
//            thirdService.checkOrder(1);
//        });
        //TODO 给客户端广播
        return equip_UpLevelService.handle(req);
    }

    @LogicMapping(desc = "装备合成",id=2)
    public G2C_EquipUpLevel equipMerge(@Validated C2G_EquipUpLevel req) {
        //根据url找脚本
        //脚本执行对应的方法，脚本可以热更新
        //context中获取playerid,channel等其他信息
        String path = "/equip/equipUpLevel";
        //TODO threadlocal使用fastthreadlocal
        EZGameCtx context = EZGameCtx.getContext();
        if(true){
            throw new EzGameException(200,"测试异常");
        }
        //分层
        int code = path.hashCode();
        //消息广播
        //消息给指定用户发
//        Method method = EquipController.class.getDeclaredField("equipUpLevel");
        return new G2C_EquipUpLevel();
    }
}
